package com.etrans.web.gps.business.log.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.log.domain.PlatformLoginRecordVo;
import com.etrans.web.gps.business.report.domain.PlatformAnalyzes;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.util.DateUtil;

/**
 * 平台登陆日志（MangoDB）持久化类
 * @author         QiLi
 * @ClassName:     PlatformLoginRecordService
 *
 */
@Service
public class PlatformLoginRecordService extends BaseService{
	
	@Autowired
	private CommonService commonService;

	public List<PlatformAnalyzes> getPlatformLoginRecordList(Integer areaId, String gnsscenterId, String startTime, String endTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		HashMap<String,Object> whereMap = new HashMap<String,Object>();
		HashMap<String,Object> dmap = new HashMap<String,Object>();
		List<PlatformAnalyzes> reslist =new ArrayList<>();
		HashMap<String,Object> sortMap=new HashMap<String,Object>();
		
		if(gnsscenterId!=null && !"".equals(gnsscenterId)){
			try {
				if (null != startTime && !"".equals(startTime)) {
					dmap.put("$gte", format.parse(startTime+" 00:00:00"));	//大于
				}
				if (null != endTime && !"".equals(endTime)) {
					dmap.put("$lte", format.parse(endTime+ " 23:59:59"));		//小于
				}
				whereMap.put("analyse_date", dmap);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(gnsscenterId!=null&&!"".equals(gnsscenterId)){
				whereMap.put("gnss_centerid",Integer.parseInt(gnsscenterId));
			}
			sortMap.put("analyse_date", 1);
			reslist = MGTemplete.find("egov", "platform_swap",whereMap,sortMap,PlatformAnalyzes.class);
			for (PlatformAnalyzes platformAnalyzes : reslist) {
				platformAnalyzes.set_id(getDateString(DateUtil.getDateToString(platformAnalyzes.getAnalyse_date(), "yyMMdd")));
			}
		}
		return reslist;
	}
	
	private String getDateString(String id){
		String str=id.substring(2, 3).equals("0")? id.substring(3,4)+"月" : id.substring(2,4)+"月";
		str+=id.substring(4, 5).equals("0")? id.substring(5)+"日" : id.substring(4)+"日";
		return str;
	}

	/**
	 * 一天的链路状态折线图
	 * @param gnsscenterId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<PlatformLoginRecordVo> getShowLineChart(String gnsscenterId, String startTime, String endTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//处理查询条件
		HashMap<String,Object> whereMap = new HashMap<String, Object>();
		HashMap<String,Object> whereMap_Time = new HashMap<String, Object>();
		HashMap<String,Object> param = new HashMap<String, Object>();
		List<PlatformLoginRecordVo> list=new ArrayList<>();
		if ( null != gnsscenterId && !"".equals(gnsscenterId) && null != startTime && !"".equals(startTime)) {
			try {
				if ( null != gnsscenterId && !"".equals(gnsscenterId)) {
					whereMap.put("gnsscenter_id", Integer.parseInt(gnsscenterId));
				}
				if (null != startTime && !"".equals(startTime)) {
					whereMap_Time.put("$gte", format.parse(startTime+" 00:00:00"));	//大于
				}
				if (null != endTime && !"".equals(endTime)) {
					whereMap_Time.put("$lte", format.parse(endTime+ " 23:59:59"));		//小于
				}
				whereMap.put("time", whereMap_Time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			list = MGTemplete.find("egov", "sub_link_log", whereMap, PlatformLoginRecordVo.class, false);
			param.put("platfromid", gnsscenterId);
			String platformName = commonService.selectplatformName(param);
			for (PlatformLoginRecordVo vo : list) {
				vo.set_id(platformName);
				String timeStr = format.format(vo.getTime());
				vo.setTimeStr(timeStr);
			}
		}
		return list;
	}

}
